
package org.jvending.provisioning.stocking.tool;

import org.jvending.provisioning.impl.ProvisioningContextImpl;
import org.jvending.provisioning.impl.AdapterRepositoryWrapper;
import org.jvending.provisioning.config.DeviceRepository;
import org.jvending.provisioning.config.AdapterRepository;
import org.jvending.registry.RepositoryRegistry;
import org.jvending.registry.hibernate.HibernateDAORegistry;

import javax.provisioning.ProvisioningContext;
import javax.provisioning.BundleRepository;
import javax.provisioning.adapter.AdapterContext;

import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author Shane Isbell
 * @since 2.0.0
 */

public class ParStocker {

    private static Logger logger = Logger.getLogger("ParStocker");

    private ProvisioningContext ctx;

    public void loadConfig(String baseDir) throws IOException {
        ctx = createProvisioningContext(baseDir);
    }

    public static void main(String[] args) throws IOException {
        if (args.length != 2) {
            logger.severe("Should only have one arg for the fileName");
            return;
        }
        ParStocker parStocker = new ParStocker();
        String fileName = args[0];
        String baseDir = args[1];
        parStocker.loadConfig(baseDir);
        long parId = parStocker.addParFile(fileName);
        logger.info("Add par file: Par ID = " + parId);
    }

    public long addParFile(String fileName) throws IOException {
        FileInputStream fis = new FileInputStream(fileName);
        byte[] parFile = copyStreamToBytes(fis);
        try {
            fis.close();
        } catch (IOException e) {
            logger.log(Level.INFO, "Could not close file: File Name = " + fileName, e);
        }
        BundleRepository repository = ctx.getBundleRepository();

        long parId = repository.addParFile(new ByteArrayInputStream(parFile));

        logger.info("Added Par File: Par Id = " + parId + ", File Name = " + fileName);
        return parId;
    }

    private ProvisioningContext createProvisioningContext(String baseDir) {
        RepositoryRegistry repositoryRegistry = RepositoryRegistry.Factory.create();
        try {
            repositoryRegistry.loadFromFile(baseDir);
        } catch (IOException e) {
            e.printStackTrace();
        }
            HibernateDAORegistry hibernateDaoRegistry = HibernateDAORegistry.Factory.create();
            hibernateDaoRegistry.setRepositoryRegistry(repositoryRegistry);


        DeviceRepository deviceRepository = (DeviceRepository) repositoryRegistry.find("devices");
       // AdapterRepository adapterRepository = (AdapterRepository) repositoryRegistry.find("adapters");
        ProvisioningContext provisioningContext = (ProvisioningContext)
                new ProvisioningContextImpl(deviceRepository, null, repositoryRegistry, hibernateDaoRegistry);

//        AdapterRepositoryWrapper adapterRepositoryWrapper =
//                new AdapterRepositoryWrapper(adapterRepository, ((AdapterContext) provisioningContext));

        return provisioningContext;
    }

    private byte[] copyStreamToBytes(InputStream is) throws IOException {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int n;
        while ((n = is.read(buffer)) >= 0) {
            os.write(buffer, 0, n);
        }
        return os.toByteArray();
    }
}
